from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, ForeignKey, Enum
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.database import Base
import enum


class ScriptType(str, enum.Enum):
    """脚本类型枚举"""
    PYTHON = "python"
    SQL = "sql"
    SHELL = "shell"
    SCALA = "scala"


class ScriptStatus(str, enum.Enum):
    """脚本状态枚举"""
    DRAFT = "draft"        # 草稿
    PUBLISHED = "published"  # 已发布
    DEPRECATED = "deprecated"  # 已废弃


class Script(Base):
    """脚本模型"""
    __tablename__ = "scripts"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, index=True)
    description = Column(Text, nullable=True)
    script_type = Column(Enum(ScriptType), nullable=False)
    content = Column(Text, nullable=False)
    status = Column(Enum(ScriptStatus), default=ScriptStatus.DRAFT)
    version = Column(String(20), default="1.0.0")
    
    # 脚本配置
    parameters = Column(Text, nullable=True)  # JSON 格式的参数配置
    dependencies = Column(Text, nullable=True)  # 依赖包列表
    timeout = Column(Integer, default=3600)  # 超时时间（秒）
    
    # 关联信息
    creator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    folder_id = Column(Integer, ForeignKey("script_folders.id"), nullable=True)
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关系
    creator = relationship("User", back_populates="scripts")
    folder = relationship("ScriptFolder", back_populates="scripts")
    versions = relationship("ScriptVersion", back_populates="script")
    executions = relationship("ScriptExecution", back_populates="script")


class ScriptFolder(Base):
    """脚本文件夹模型"""
    __tablename__ = "script_folders"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    description = Column(Text, nullable=True)
    parent_id = Column(Integer, ForeignKey("script_folders.id"), nullable=True)
    creator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    parent = relationship("ScriptFolder", remote_side=[id])
    children = relationship("ScriptFolder")
    scripts = relationship("Script", back_populates="folder")
    creator = relationship("User")


class ScriptVersion(Base):
    """脚本版本模型"""
    __tablename__ = "script_versions"

    id = Column(Integer, primary_key=True, index=True)
    script_id = Column(Integer, ForeignKey("scripts.id"), nullable=False)
    version = Column(String(20), nullable=False)
    content = Column(Text, nullable=False)
    change_log = Column(Text, nullable=True)
    creator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    script = relationship("Script", back_populates="versions")
    creator = relationship("User")


class ScriptExecution(Base):
    """脚本执行记录模型"""
    __tablename__ = "script_executions"

    id = Column(Integer, primary_key=True, index=True)
    script_id = Column(Integer, ForeignKey("scripts.id"), nullable=False)
    executor_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    
    # 执行信息
    status = Column(String(20), default="running")  # running, success, failed, cancelled
    parameters = Column(Text, nullable=True)  # 执行参数
    start_time = Column(DateTime(timezone=True), server_default=func.now())
    end_time = Column(DateTime(timezone=True), nullable=True)
    duration = Column(Integer, nullable=True)  # 执行时长（秒）
    
    # 执行结果
    output = Column(Text, nullable=True)
    error_message = Column(Text, nullable=True)
    exit_code = Column(Integer, nullable=True)
    
    # 资源使用情况
    cpu_usage = Column(String(50), nullable=True)
    memory_usage = Column(String(50), nullable=True)
    
    # 关系
    script = relationship("Script", back_populates="executions")
    executor = relationship("User")


class ScriptTemplate(Base):
    """脚本模板模型"""
    __tablename__ = "script_templates"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    description = Column(Text, nullable=True)
    script_type = Column(Enum(ScriptType), nullable=False)
    template_content = Column(Text, nullable=False)
    category = Column(String(50), nullable=True)  # 模板分类
    is_public = Column(Boolean, default=False)  # 是否公开
    
    creator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 关系
    creator = relationship("User") 